package com.aosai.localtax.adapter;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aosai.localtax.R;
import com.aosai.localtax.activity.ArticleDetailsActivity;
import com.aosai.localtax.model.MainViewPagerModel;

import java.util.ArrayList;
import java.util.List;

import pw.hais.http.Http;
import pw.hais.utils.L;

/**
 * Created by hais1992 on 2016/1/16/016.
 */
public class ViewPagerAdapter extends PagerAdapter {
    private List<MainViewPagerModel> mDatas;
    private List<View> mViews;
    private OnClickListener listener;
    private Context mContext;

    public ViewPagerAdapter(Context mContext, List<MainViewPagerModel> mDatas, OnClickListener listener) {
        this.listener = listener;
        this.mContext = mContext;
        mViews = new ArrayList<>();
        this.mDatas = mDatas;

        int length = mDatas == null ? 0 : mDatas.size();

        for (int i = 0; i < length; i++) {
            mViews.add(new RelativeLayout(mContext));
        }
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == (arg1);
    }

    @Override
    public Object instantiateItem(View container, final int position) {
        ViewGroup containers = (ViewGroup) mViews.get(position);

        View v = LayoutInflater.from(containers.getContext()).inflate(R.layout.activity_main_viewpage_item, null,false);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ArticleDetailsActivity.class);
                intent.putExtra("Article", mDatas.get(position));
                mContext.startActivity(intent);
            }
        });

        ImageView imageView = (ImageView)v.findViewById(R.id.image_icon);
        TextView textView = (TextView) v.findViewById(R.id.text_title);

        Http.displayImageFullSize(imageView, mDatas.get(position).image_url);
        textView.setText(mDatas.get(position).title);

        TextView subTitleTv=(TextView)v.findViewById(R.id.text_subTitle);
        subTitleTv.setText(mDatas.get(position).subTitle);

                ((ViewPager) container).addView(v, 0);
        return v;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(mViews.get(position));
    }
}

